package classes;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import containers.Page;

public class PageRetriever {
	private boolean converted;
	private Set<String> myVisitedPages; /**Set for visited pages.*/
	
	public PageRetriever() {
		converted = false;
		myVisitedPages = new HashSet<String>();
	}
	
	public Page getPageFromURL(final URL url) {
		//determine if can successfully convert url to page
		Page newPage = null;
		if (validateURL(url) == true && myVisitedPages.add(url.toString()) == true) {
			newPage = convertURLtoPage(url);
			//send page to pageQ
			if (!converted) {
				newPage = null;
			}
		}
		
		return newPage;
	}
	
	private Page convertURLtoPage(final URL url) {
		String currStringURL = new String();
		Page currPage = new Page(url);
		InputStream iStream = null;
		BufferedReader bReader;
		String myString = "";
		StringBuilder sBuilder = new StringBuilder();
		converted = true;
		
		try {
			currStringURL = url.toString();
			iStream = url.openStream();
			bReader = new BufferedReader(new InputStreamReader(iStream));
			while ((myString = bReader.readLine()) != null) {
				sBuilder.append(myString);
			}
			currPage.buildRetrievedPage(url.toString(), sBuilder.toString());
		} catch (IOException e) {
			converted = false;
		}
		
		return currPage;
	}
	
	private synchronized boolean validateURL(final URL theURL) {
		boolean valid = false;
		String mimeType = new String();
		try {
			mimeType = theURL.openConnection().getContentType();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		if ( mimeType.equals("text/html") || mimeType.equals("text/plain")) {
			valid = true;
		}
		
		return valid;		
	}
	
}
